package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.OrderMapper;
import cn.supermarket.pojo.dto.AddOrderDTO;
import cn.supermarket.pojo.dto.OrderItemDTO;
import cn.supermarket.pojo.entity.OrderItem;
import cn.supermarket.pojo.entity.PurchaseOrders;
import cn.supermarket.pojo.vo.PurchaseOrdersVO;
import cn.supermarket.pojo.vo.SimpleOrderVO;
import cn.supermarket.service.iservice.IOrderService;
import cn.supermarket.utils.GetDateTime;
import cn.supermarket.utils.GetInfoUtils;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;

import cn.supermarket.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GetDateTime getDateTime;
    @Autowired
    private GetInfoUtils getInfoUtils;

    @Override
    public void addOrder(AddOrderDTO addOrderDTO) {
        //获取id
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker = new SnowFlakeGenerateIdWorker(12L, 20L);
        Long id = snowFlakeGenerateIdWorker.nextId();
        //创建list
        List<OrderItemDTO> orderItemDTOS = addOrderDTO.getOrderItemDTOS();
        System.out.println("*************" + orderItemDTOS);
        List<OrderItem> orderItems = new ArrayList<>();
        BigDecimal orderMoney = new BigDecimal(0);
        BigDecimal itemMoney;
        for (OrderItemDTO orderItemDTO : orderItemDTOS) {
            OrderItem orderItem = new OrderItem();
            //计算价格
            orderMoney = orderMoney.add(orderItemDTO.getProductNum().multiply(orderItemDTO.getUnitPrice()));
            itemMoney = orderItemDTO.getProductNum().multiply(orderItemDTO.getUnitPrice());
            //将用户输入的商品信息属性添加全面
            BeanUtils.copyProperties(orderItemDTO, orderItem);
            orderItem.setTotalPrice(itemMoney);
            orderItem.setId(snowFlakeGenerateIdWorker.nextId());
            orderItem.setOrderId(id);
            orderItems.add(orderItem);
            System.out.println("单项" + orderItem);
        }
        System.out.println("集合" + orderItems);
        System.out.println("--------------" + orderItems);
        PurchaseOrders purchaseOrders = new PurchaseOrders();
        BeanUtils.copyProperties(addOrderDTO, purchaseOrders);
        purchaseOrders.setId(snowFlakeGenerateIdWorker.nextId());
        purchaseOrders.setOrderId(id);
        purchaseOrders.setAllTotalPrice(orderMoney);
        LocalDateTime now = LocalDateTime.now();
        purchaseOrders.setCreateData(now);
        purchaseOrders.setModifyData(now);
        orderMapper.insertOrder(purchaseOrders);
        System.out.println("===================" + orderItems);
        orderMapper.insertOrderItem(orderItems);
    }

    @Override
    public List<PurchaseOrdersVO> selectOrderByIdOrByTime(Long id, Date startTime, Date endTime) {
        if (id==null&&(startTime==null||endTime==null)){
            throw new ServiceException(State.ERR_BAD_REQUEST,"请输入查询条件是否正确");
        }
        if (startTime.after(endTime)){
            throw new ServiceException(State.ERR_BAD_REQUEST,"输入日期错误");
        }
        if(id!=null){
            List<PurchaseOrdersVO> list = orderMapper.selectOrderById(id);
            if (list.isEmpty()){
                throw new ServiceException(State.ERR_BAD_REQUEST,"未查询到订单信息");
            }
            return list;
        }
        LocalDateTime dateStart = getDateTime.getDateStart(startTime);
        LocalDateTime dateEnd = getDateTime.getDateEnd(endTime);
        List<PurchaseOrdersVO> list = orderMapper.selectOrderByTime(dateStart, dateEnd);
        for (PurchaseOrdersVO purchaseOrdersVO:list){
            System.out.println(purchaseOrdersVO+"!!!!!!!!!!!!!!!!!!!!!!!");
            if (purchaseOrdersVO.getIsDisplay()==0&&purchaseOrdersVO.getIsCancel()==1){
                list.remove(purchaseOrdersVO);
            }
            if (list.isEmpty()){
                throw new ServiceException(State.ERR_BAD_REQUEST,"未查询到订单信息");
            }
        }

        return list;
    }

    @Override
    public List<SimpleOrderVO> selectOrderByPurchaser(String purchaser) {
        if (purchaser==null){
            throw new ServiceException(State.ERR_BAD_REQUEST,"请检查您输入的供货商信息");
        }
        List<SimpleOrderVO> simpleOrders = orderMapper.selectOrderByPurchaser(purchaser);
        if (simpleOrders.isEmpty()){
            throw new ServiceException(State.ERR_BAD_REQUEST,"未查询到您所查询的订单");
        }
        return simpleOrders;
    }



    /**
     * 根据订单编号修改订单是否可见
     * @param orderId
     * @param isDisplay
     */
    public void updateOrderIsDisplay(Long orderId,Integer isDisplay){
        /*String modifyName=getInfoUtils.getUserInfo().getUsername();*/
        String modifyName ="1111";
        int row = orderMapper.updateOrderIsDisplay(orderId, isDisplay,modifyName);
        if (row==0){
            throw new ServiceException(State.ERR_ILLEGAL_CONTROLLER,"订单可能已被锁定或者作废不可被修改");
        }
    }

    /**
     * 根据订单编号修改订单是否锁定
     * @param orderId
     * @param isEnable
     */
    public void updateOrderIsEnable(Long orderId,Integer isEnable){
//        String modifyName=getInfoUtils.getUserInfo().getUsername();
       /* GetInfoUtils info = getInfoUtils.getUserInfo();
        String modifyName = info.getUsername();*/
        String modifyName ="1111";
        int row = orderMapper.updateOrderIsEnable(orderId, isEnable,modifyName);
        if (row==0){
            throw new ServiceException(State.ERR_ILLEGAL_CONTROLLER,"订单可能已被锁定或者作废不可被修改");
        }
    }

    /**
     * 根据订单编号修改是否作废
     * @param orderId
     * @param isCancel
     */
    public void updateOrderIsCancel(Long orderId,Integer isCancel){
       /* String modifyName=getInfoUtils.getUserInfo().getUsername();*/
        String modifyName ="1111";
        int row = orderMapper.updateOrderIsCancel(orderId, isCancel,modifyName);
        if (row==0){
            throw new ServiceException(State.ERR_ILLEGAL_CONTROLLER,"订单可能已被锁定或者作废不可被修改");
        }
    }


}


